﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour {

    public VIPJoystick stick;
    public Transform cameraTf;
    public Vector3 pos;
    private Rigidbody playerRigidbody;
    public float speed;

    float yDegree;

    private void Awake()
    {

        playerRigidbody = gameObject.GetComponent<Rigidbody>();
    }

    private void Start()
    {
        //移除场景剔除
        //CullingGroup cg = new CullingGroup();
        Init();
    }

    void Init() {
        pos = new Vector3(-5, 5, -5);
        speed = 5f;
    }

    private void Update()
    {
        
    }

    private void FixedUpdate() {
        //弧度tan，正切
        float rotate = Mathf.Atan2(stick.dir.y, stick.dir.x);
        //弧度转角度
        float degree = rotate * Mathf.Rad2Deg;
        degree = 360 - degree + 90;
        if (stick.dir.x != 0 && stick.dir.y != 0)
        {
            yDegree = degree;
        }
        //旋转
        Vector3 rosV3 = transform.localEulerAngles;
        rosV3.y = yDegree;
        playerRigidbody.MoveRotation(Quaternion.Euler(rosV3));
        //移动
        Vector3 posV3 = new Vector3(stick.dir.x, 0, stick.dir.y);
        playerRigidbody.MovePosition(transform.localPosition + posV3 * Time.deltaTime * speed);
        //摄像头看像主角
        cameraTf.rotation = Quaternion.Slerp(cameraTf.rotation, Quaternion.LookRotation(transform.position - cameraTf.position), Time.deltaTime);
        //摄像头跟踪主角
        cameraTf.transform.position = Vector3.Lerp(cameraTf.transform.position, transform.position + pos, Time.deltaTime);
    }

}
